.TH std::basic_string::pop_back 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::basic_string::pop_back \- std::basic_string::pop_back

.SH Synopsis
   void pop_back();  (constexpr since C++20)

   Removes the last character from the string.

   Equivalent to erase(end() - 1). The behavior is undefined if the string is empty.

.SH Parameters

   \fI(none)\fP

.SH Return value

   \fI(none)\fP

.SH Complexity

   Constant.

.SH Exceptions

   Throws nothing.

.SH Notes

   In libstdc++, pop_back() is not available in C++98 mode.

.SH Example


// Run this code

 #include <cassert>
 #include <iomanip>
 #include <iostream>
 #include <string>

 int main()
 {
     std::string str("Short string!");
     std::cout << "before=" << std::quoted(str) << '\\n';
     assert(str.size() == 13);

     str.pop_back();
     std::cout << " after=" << std::quoted(str) << '\\n';
     assert(str.size() == 12);

     str.clear();
 //  str.pop_back(); // undefined behavior
 }

.SH Output:

 before="Short string!"
  after="Short string"

   Defect reports

   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.

     DR    Applied to              Behavior as published               Correct behavior
   LWG 534 C++98      std::basic_string did not have the member        added
                      function pop_back()

.SH See also

   push_back appends a character to the end
             \fI(public member function)\fP
   erase     removes characters
             \fI(public member function)\fP
